package com.gravata.enigma.activities;

import java.sql.SQLException;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.Toast;

import com.gravata.enigma.R;
import com.gravata.enigma.model.Profile;
import com.gravata.enigma.model.Regra;

public class ProfileActivity extends Activity {

	private EditText nome,usuario,senha,porta,portaHttp,satelite,lamedb,ip;
	private Profile profile;
	private boolean novo=false;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.profile_screen);

		nome=(EditText) findViewById(R.id.profile_nome);
		usuario=(EditText) findViewById(R.id.profile_usuario);
		senha=(EditText) findViewById(R.id.profile_senha);
		porta=(EditText) findViewById(R.id.profile_porta);
		portaHttp=(EditText) findViewById(R.id.profile_porta_http);
		satelite=(EditText) findViewById(R.id.profile_satelite);
		lamedb=(EditText) findViewById(R.id.profile_lamedb);
		ip=(EditText) findViewById(R.id.profile_url);

		if(getIntent().getSerializableExtra("profile")!=null){
			profile=(Profile) getIntent().getSerializableExtra("profile");

			nome.setText(profile.getNome());
			usuario.setText(profile.getUsuario());
			senha.setText(profile.getSenha());
			porta.setText(Integer.toString(profile.getPorta()));
			portaHttp.setText(Integer.toString(profile.getPortaHttp()));
			satelite.setText(profile.getSatelitePath());
			lamedb.setText(profile.getLamePath());
			ip.setText(profile.getUrl());

		}else{
			profile=new Profile();
			novo=true;
		}

	}

	private boolean validate(){
		if(nome.getText().toString().isEmpty() || usuario.getText().toString().isEmpty() ||
				senha.getText().toString().isEmpty() || portaHttp.getText().toString().isEmpty() || porta.getText().toString().isEmpty() ||
				satelite.getText().toString().isEmpty() || lamedb.getText().toString().isEmpty()|| ip.getText().toString().isEmpty() )
			return false;
		return true;
	}

	private void salvar(){
		if(validate()){
			profile.setNome(nome.getText().toString());
			profile.setUrl(ip.getText().toString());
			profile.setLamePath(lamedb.getText().toString());
			profile.setSatelitePath(satelite.getText().toString());
			profile.setUsuario(usuario.getText().toString());
			profile.setSenha(senha.getText().toString());
			profile.setPorta(Integer.parseInt(porta.getText().toString()));
			profile.setPortaHttp(Integer.parseInt(portaHttp.getText().toString()));


			try {
				TelaInicial.repositorioProfile.inserirOuAtualizar(profile);

				if(novo){

					Regra regraE=new Regra();
					regraE.setProfile(profile);
					regraE.setName("EXCLUDES");
					regraE.setExclude(true);
					regraE.setOrdem(0);
					TelaInicial.repositorioRegra.inserir(regraE);

					Regra regra=new Regra();
					regra.setProfile(profile);
					regra.setName("Favourites (TV)");
					regra.setFavorito(true);
					regra.setOrdem(1);
					TelaInicial.repositorioRegra.inserir(regra);

				}

				if(profile.isSelecionado())
					TelaInicial.profile=profile;

				setResult(1, getIntent());
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				setResult(0, getIntent());
				Toast.makeText(this, R.string.erro_salvar, Toast.LENGTH_LONG).show();

			}
			finish();

		}else{
			Toast.makeText(this, R.string.preencha_todos, Toast.LENGTH_LONG).show();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.regra, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case R.id.menu_regra_salvar:
			salvar();
			break;
		case R.id.menu_regra_cancelar:

			setResult(0, getIntent());
			finish();

			break;
		default:
			return super.onOptionsItemSelected(item);
		}

		return true;

	}
}
